#!/usr/bin/env python
# -*- coding: utf-8 -*

from lib.textprocessor import *

import locale,sys,traceback
import _curses, curses,curses.ascii,types
import lib.keys as keys
from lib.textfileopen import f_open

locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()

try:
    stdscr = curses.initscr()
    y,x=stdscr.getmaxyx()
    screen_size = Rectangle(7,0,x-7,y-1) 
    curses.noecho();curses.cbreak();stdscr.keypad(0)
    
    curses.halfdelay(2)
    key,char=0,""
    
    curses.curs_set(2)
    curses.start_color()
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)

    text = f_open("../pp.py")
        	    
    tp = TextProcessor(text)
    pp = PageProcessor(screen_size,tp)

    keyss = ["-1","-1","-1","-1","-1","-1"]
    pp.build(stdscr)
    pp.Cursor(stdscr)
    while char!='esc':
	key=stdscr.getch()
	if (key<256):
	    char = keys.parseKey(key)
	    if (char=="down"):tp.cursor.y+=1;pp.Cursor(stdscr)
	    elif (char=="up"):tp.cursor.y-=1;pp.Cursor(stdscr)
	    elif (char=="left"):tp.cursor.x-=1;pp.Cursor(stdscr)
	    elif (char=="right"):tp.cursor.x+=1;pp.Cursor(stdscr)

	
except Exception,e:
    pass
finally:
    curses.nocbreak(); stdscr.keypad(0); curses.echo()
    curses.endwin()

traceback.print_exc(file=sys.stdout)
	
